﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QuanLyBanHang.QuerySQL;
using DevComponents.DotNetBar;

namespace QuanLyBanHang
{
    public partial class frqlDathang : Office2007Form
    {
        ClassqlDathang qldh = new ClassqlDathang();
        private int dong;
        public frqlDathang()
        {
            InitializeComponent();
        }

        private void frqlDathang_Load(object sender, EventArgs e)
        {
            dtgv_DH.DataSource = qldh.ShowDH();
            dtgv_CTDH.DataSource = qldh.ShowCTHD(0);
        }

        private void dtgv_DH_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            try
            {
                for (int i = 0; i < dtgv_DH.RowCount; i++)
                {
                    dtgv_DH.Rows[i].Cells["clhd_stt"].Value = i + 1;
                }
            }
            catch { }
        }

        private void dtgv_CTDH_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            try
            {
                for (int i = 0; i < dtgv_CTDH.RowCount; i++)
                {
                    dtgv_CTDH.Rows[i].Cells["clct_stt"].Value = i + 1;
                }
            }
            catch { }
        }

        private void dtgv_DH_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
            dtgv_CTDH.DataSource = qldh.ShowCTHD(1, dtgv_DH.Rows[dong].Cells["clhd_mahd"].Value.ToString());
        }

        private void txtmahd_TextChanged(object sender, EventArgs e)
        {
            dtgv_DH.DataSource = qldh.TKHD(0,txtmahd.Text);
        }

        private void txttenkh_TextChanged(object sender, EventArgs e)
        {
            dtgv_DH.DataSource = qldh.TKHD(1, txtmahd.Text,txttenkh.Text);
        }

        private void btnhuy2_Click(object sender, EventArgs e)
        {
            txtmahd.Clear();
            txttenkh.Clear();
        }

        private void txtctmahd_TextChanged(object sender, EventArgs e)
        {
            dtgv_CTDH.DataSource = qldh.ShowCTHD(1, txtctmahd.Text);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult kq = MessageBox.Show("Bạn có chắc muốn xóa Hóa đơn " + dtgv_DH.Rows[dong].Cells["clhd_mahd"].Value.ToString() + "  ? ", "Thông báo", MessageBoxButtons.YesNo);
            if (kq == DialogResult.Yes)
            {

                qldh.XoaCTDH(dtgv_DH.Rows[dong].Cells["clhd_mahd"].Value.ToString());
                qldh.XoaDH(dtgv_DH.Rows[dong].Cells["clhd_mahd"].Value.ToString());
                frqlDathang_Load(sender, e);
            }
        }
    }
}
